/*eslint-disable*/
import nowTime from 'nowTime';

type tomato={
    [data:string]:UserTime
}
type UserTime={
    note:string,
    time:string,
}

const useTomato=()=>{
    const getTomato=()=>{
        let a:Array<tomato> = JSON.parse(localStorage.getItem('tomato')||'[]')
        return a  
    }
    const addTomato=(val:UserTime | undefined)=>{
        if(val===undefined){
            return
        }else{
            let a = getTomato()
            let date = nowTime()
            a.push({[date]:val})
            localStorage.setItem('tomato',JSON.stringify(a))
            window.alert('添加成功')
        }
    }
    const rmTomato=(time:string,note:UserTime)=>{
        let a =getTomato()
        let index=0
        a.map((items)=>{
            if(Object.keys(items)[0]===time ){
                if(items[Object.keys(items)[0]].note===note.note){
                    index=a.indexOf(items)
                }
            }
        })
        a.splice(index,1)
        localStorage.setItem('tomato',JSON.stringify(a))
        window.alert('删除成功')
        window.location.reload()
    }
    return { getTomato,addTomato ,rmTomato}
}

export default useTomato